#include "RunningChartBar.h"

RunningChartBar::RunningChartBar()
{
    _scrollArea = new QScrollArea();
    _chart = new QChart();
    _chartview = new QChartView();
    _scrollArea->setWidget(_chartview);
    _chartview->setFixedWidth(5000);
    _anchor = nullptr;
}


void RunningChartBar::load(){

    QObject *pRoot = engine->rootObjects().first();

    QQuickItem *chartItem = pRoot->findChild<QQuickItem *>("RunningChartBar");

    QQuickWindow* qmlwindow = qobject_cast<QQuickWindow *>(engine->rootObjects().first());
    QWindow* chartWindow = QWindow::fromWinId(_scrollArea->winId());

    chartWindow->setParent(qmlwindow);

    _chartview->setChart(_chart);

    QVector<QPointF> pointList;
    //    _chart->appendCurve("string1", pointList, "spline");
    _scrollArea->show();
    _anchor = new WidgetAnchor(_scrollArea, chartItem);

    _scrollArea->setStyleSheet("background: rgba(255,255,255, 1); border: 0px solid #FFFFFF;");

    _axis = new QBarCategoryAxis();
    _series = new QBarSeries();
    _chart->legend()->hide();
    _chart->addAxis(_axis, Qt::AlignBottom);


    _axis_y = new QValueAxis();
    _chart->addAxis(_axis_y, Qt::AlignLeft);
    _axis_y->setRange(0, 10);

    _axis_y->setTitleText("荧光强度(Ru)");
//    _axis->setTitleText("孔位");


    connect(_anchor, SIGNAL(sigRectChanged(int,int,int,int)), this, SLOT(onAnchorRectChanged(int,int,int,int)));

}


void RunningChartBar::onAnchorRectChanged(int x, int y, int width, int height){


    _chartview->setFixedHeight(_scrollArea->height() - 30);
    _scrollArea->setWidget(_chartview);
    _chartview->setFixedWidth(std::max(_chartview->width(), _scrollArea->width()-50));
}

void RunningChartBar::show(){
    if(_anchor)
        _anchor->show();
}

void RunningChartBar::hide(){
    if(_anchor)
        _anchor->hide();
}

void RunningChartBar::refresh(QString equId, QString accessInfo, QString posInfo){

    QMap<QString, QVariantList> res = expPro->getBarChartInfo(equId, accessInfo, posInfo);

    int maxVal = 0;

    QVariantList posiList = res["posi"];
    QVariantList FAMList = res["FAM"];
    QVariantList VICList = res["VIC"];
    QVariantList ROXList = res["ROX"];
    QVariantList CY5List = res["CY5"];

    _chartview->setFixedWidth(std::max(posiList.size() * 150, _scrollArea->width()-50));
    _series->clear();
    _series = new QBarSeries;
    _chart->removeAllSeries();

    set_FAM = new QBarSet("FAM");
    set_ROX = new QBarSet("ROX");
    set_CY5 = new QBarSet("CY5");
    set_VIC = new QBarSet("VIC");
    set_FAM->setColor("#1677FF");
    set_ROX->setColor("#EBA23C");
    set_VIC->setColor("#67C23A");
    set_CY5->setColor("#F56C6C");

    // 4
    QStringList categories;
    for(int i = 0; i < posiList.size(); i ++){
        categories.push_back(posiList[i].toString());
        if(i < FAMList.size()){
            *set_FAM << FAMList[i].toInt();
        }
        if(i < VICList.size())   *set_VIC << VICList[i].toInt();
        if(i < ROXList.size())   *set_ROX << ROXList[i].toInt();
        if(i < CY5List.size())   *set_CY5 << CY5List[i].toInt();

        maxVal = std::max(maxVal, FAMList[i].toInt());
        maxVal = std::max(maxVal, VICList[i].toInt());
        maxVal = std::max(maxVal, ROXList[i].toInt());
        maxVal = std::max(maxVal, CY5List[i].toInt());
    }

    if(res["FAM"].size() != 0) {
        _series->append(set_FAM);
    }
    if(res["ROX"].size() != 0) _series->append(set_ROX);
    if(res["VIC"].size() != 0) _series->append(set_VIC);
    if(res["CY5"].size() != 0) _series->append(set_CY5);

    // 3
    _chart->addSeries(_series);
    _axis->clear();
    _axis->append(categories);

    _axis_y->setRange(0, std::max(maxVal, 10));

}
